//
//  IDAddressPickerView.m
//  IDAddressPickView
//
//  Created by Island on 16/7/15.
//  Copyright © 2016年 Island. All rights reserved.
//

#import "IDAddressPickerView.h"

#define Province @"province"
#define ProvinceId @"provinceId"
#define City @"city"
#define CityId @"cityId"
#define Area @"area"
#define AreaId @"areaId"

@interface IDAddressPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>
/** 地址选择器 */
@property (nonatomic, strong) UIPickerView *pickerView;
/** 选中的省份 */
@property (nonatomic, assign) NSInteger provinceIndex;
/** 选中的城市 */
@property (nonatomic, assign) NSInteger cityIndex;
/** 选中的省份 */
@property (nonatomic, assign) NSInteger areaIndex;
@end

@implementation IDAddressPickerView

#pragma mark - initializer
- (instancetype)initWithFrame:(CGRect)frame {
    // 判断用户初始化时是否设置了 frame
    if (CGRectEqualToRect(frame, CGRectNull) || CGRectEqualToRect(frame, CGRectZero)) {
        // 只需要设置 height，x,y,width 不会产生影响
        frame = CGRectMake(0, 0, 0, 200);
    }
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.pickerView];
    }
    
    return self;
}

- (void)setRow:(NSDictionary *)dic{
    int i = 0;
    int j = 0;
    int k = 0;
    for (i=0; i<_addressArray.count; i++) {
        NSDictionary *d1 = _addressArray[i];
        if ([d1[@"id"] intValue] == [dic[@"province"] intValue]) {
            self.selectedAddress[Province] = d1[@"name"];
            self.selectedAddress[ProvinceId] = d1[@"id"];
            
            for (j=0; j<[d1[@"city"] count]; j++) {
                NSDictionary *d2 = d1[@"city"][j];
                if ([d2[@"id"] intValue] == [dic[@"city"] intValue]) {
                    self.selectedAddress[City] = d2[@"name"];
                    self.selectedAddress[CityId] = d2[@"id"];
                    
                    for (k=0; k<[d2[@"district"] count]; k++) {
                        NSDictionary *d3 = d2[@"district"][k];
                        if ([d3[@"id"] intValue] == [dic[@"district"] intValue]) {
                            self.selectedAddress[Area] = d3[@"name"];
                            self.selectedAddress[AreaId] = d3[@"id"];
                            
                            break;
                        }
                    }
                    break;
                }
            }
            break;
        }
    }
    self.provinceIndex = i;
    self.cityIndex = j;
    [self.pickerView selectRow:i inComponent:0 animated:NO];
    [self.pickerView selectRow:j inComponent:1 animated:NO];
    [self.pickerView selectRow:k inComponent:2 animated:NO];
    [self.dataSource setData:self.selectedAddress];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    //
    if (!self.selectedAddress.count) {
        NSDictionary *province = _addressArray[0];
        NSDictionary *city = province[@"city"][0];
        self.selectedAddress[Province] = _addressArray[0][@"name"];
        self.selectedAddress[ProvinceId] = _addressArray[0][@"id"];
        if ([province[@"city"] count] > 0) {
            self.selectedAddress[City] = province[@"city"][0][@"name"];
            self.selectedAddress[CityId] = province[@"city"][0][@"id"];
        } else {
            self.selectedAddress[City] = @"";
            self.selectedAddress[CityId] = 0;
        }
        if ([city[@"district"] count] > 0) {
            self.selectedAddress[Area] = city[@"district"][0][@"name"];
            self.selectedAddress[AreaId] = city[@"district"][0][@"id"];
        } else {
            self.selectedAddress[Area] = @"";
            self.selectedAddress[AreaId] = 0;
        }
    }
    [self.dataSource setData:self.selectedAddress];
    //
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSInteger numberOfRowsInComponent = 0;
    switch (component) {
        case 0:
            numberOfRowsInComponent = self.addressArray.count;
            break;
        case 1:
        {
            NSDictionary *province = self.addressArray[self.provinceIndex];
            numberOfRowsInComponent = [province[@"city"] count];
        }
            break;
        case 2:
        {
            NSDictionary *province = self.addressArray[self.provinceIndex];
            NSDictionary *cities = province[@"city"][self.cityIndex];
            numberOfRowsInComponent = [cities[@"district"] count];
        }
            break;
    }
    return numberOfRowsInComponent;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *titleForRow = @"";
    switch (component) {
        case 0:
            titleForRow = self.addressArray[row][@"name"];
            break;
        case 1:
        {
            NSDictionary *province = self.addressArray[self.provinceIndex];
            titleForRow = province[@"city"][row][@"name"];
        }
            break;
        case 2:
        {
            NSDictionary *province = self.addressArray[self.provinceIndex];
            NSDictionary *city = province[@"city"][self.cityIndex];
            titleForRow = city[@"district"][row][@"name"];
        }
            break;
    }
    return titleForRow;
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    switch (component) {
        case 0:
        {
            self.provinceIndex = row;
            self.cityIndex = 0;
            self.areaIndex = 0;
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:1 animated:NO];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            /**
             *  更新选中的 addresss，包括：市，区
             */
            NSDictionary *province = self.addressArray[self.provinceIndex];
            NSDictionary *city = province[@"city"][self.cityIndex];
            self.selectedAddress[Province] = self.addressArray[row][@"name"];
            self.selectedAddress[ProvinceId] = self.addressArray[row][@"id"];
            if ([province[@"city"] count] > 0) {
                self.selectedAddress[City] = province[@"city"][0][@"name"];
                self.selectedAddress[CityId] = province[@"city"][0][@"id"];
            } else {
                self.selectedAddress[City] = @"";
                self.selectedAddress[CityId] = 0;
            }
            if ([city[@"district"] count] > 0) {
                self.selectedAddress[Area] = city[@"district"][0][@"name"];
                self.selectedAddress[AreaId] = city[@"district"][0][@"id"];
            } else {
                self.selectedAddress[Area] = @"";
                self.selectedAddress[AreaId] = 0;
            }
        }
            break;
        case 1:
        {
            self.cityIndex = row;
            self.areaIndex = 0;
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            /**
             *  更新选中的 addresss，包括：区
             */
            NSDictionary *province = self.addressArray[self.provinceIndex];
            NSDictionary *city = province[@"city"][self.cityIndex];
            self.selectedAddress[City] = province[@"city"][row][@"name"];
            self.selectedAddress[CityId] = province[@"city"][row][@"id"];
            if ([city[@"district"] count] > 0) {
                self.selectedAddress[Area] = city[@"district"][0][@"name"];
                self.selectedAddress[AreaId] = city[@"district"][0][@"id"];
            } else {
                self.selectedAddress[Area] = @"";
                self.selectedAddress[AreaId] = 0;
            }
        }
            break;
        case 2:
        {
            self.areaIndex = row;
            /**
             *  更新选中的 addresss
             */
            NSDictionary *province = self.addressArray[self.provinceIndex];
            NSDictionary *city = province[@"city"][self.cityIndex];
            self.selectedAddress[Area] = city[@"district"][row][@"name"];
            self.selectedAddress[AreaId] = city[@"district"][row][@"id"];
        }
            break;
    }
    [self.dataSource setData:self.selectedAddress];
}

#pragma mark - getter
- (UIPickerView *)pickerView {
    if (_pickerView == nil) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
    }
    return _pickerView;
}
- (NSMutableDictionary *)selectedAddress {
    if (_selectedAddress == nil) {
        _selectedAddress = [NSMutableDictionary dictionary];
    }
    return _selectedAddress;
}
@end
